package cz.muni.fi.pa036.kiss.daoimpl;

import cz.muni.fi.pa036.kiss.Entity.Student;
import cz.muni.fi.pa036.kiss.dao.StudentDAO;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

public class StudentDAOImpl implements StudentDAO {

    @PersistenceContext
    private EntityManager em;

    public void setEntityManager(EntityManager em) {
        this.em = em;
    }

    @Override
    public void createStudent(Student student) {
        if (student == null) {
            throw new IllegalArgumentException("student is null");
        }
        if (student.getId() != null) {
            throw new IllegalArgumentException("student.id is not null");
        }

        em.persist(student);
    }

    @Override
    public Student getStudent(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("null id");
        }
        Student student = em.find(Student.class, id);
        return student;
    }

    @Override
    public void removeStudent(Student student) {
        if (student == null) {
            throw new IllegalArgumentException("student is null");
        }
        if (student.getId() == null) {
            throw new IllegalArgumentException("student.id is null");
        }
        Student c = em.find(Student.class, student.getId());
        em.remove(c);
    }

    @Override
    public void updateStudent(Student student) {
        if (student == null) {
            throw new IllegalArgumentException("student is null");
        }
        if (student.getId() == null) {
            throw new IllegalArgumentException("student.id is null");
        }

        em.merge(student);
    }

    @Override
    public List<Student> findAll() {
        Query query = em.createQuery("SELECT c FROM Student c");
        List<Student> results = query.getResultList();

        return results;
    }

    @Override
    public List<Student> findByName(String jmeno, String prijmeni) {
        boolean jmenoNull = (jmeno == null);
        boolean prijmeniNull = (prijmeni == null);
        if (jmenoNull && prijmeniNull) {
            throw new IllegalArgumentException("null jmeno and prijmeni");
        }
        Query query;
        if (jmenoNull) {
            query = em.createQuery("SELECT c FROM Student c WHERE c.prijmeni like :prijmeni");
            query.setParameter("lastName", prijmeni);
        } else if (prijmeniNull) {
            query = em.createQuery("SELECT c FROM Student c WHERE c.jmeno like :jmeno");
            query.setParameter("firstName", jmeno);
        } else {
            query = em.createQuery("SELECT c FROM Student c WHERE c.jmeno like :jmeno AND c.prijmeni like :prijmeni");
            query.setParameter("lastName", prijmeni);
            query.setParameter("firstName", jmeno);
        }
        List<Student> results = query.getResultList();
        return results;
    }
}
